<template>
  <div id="app">
    <header class="app-header">
      <h1>点云数据查看器</h1>
      <div class="file-controls">
        <button @click="openFileDialog" class="btn btn-primary">
          📁 选择点云js文件
        </button>
      </div>
    </header>
    <main class="app-main">
      <PointCloudViewer 
        :file-url="currentFileUrl"
        @file-loaded="handleFileLoaded"
        @error="handleError"
      />
    </main>
    <div v-if="error" class="error-toast">
      <div class="error-content">
        <span class="error-icon">⚠️</span>
        <span class="error-text">{{ error }}</span>
        <button @click="clearError" class="error-close">×</button>
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue'
import PointCloudViewer from './components/PointCloudViewer.vue'

export default {
  name: 'App',
  components: { PointCloudViewer },
  setup() {
    const currentFileName = ref('')
    const currentFileContent = ref('')
    const currentFileUrl = ref('') // 添加这个用于传递URL给PointCloudViewer
    const error = ref('')

    // 只支持js文件
    const openFileDialog = async () => {
      if (window.electronAPI) {
        const result = await window.electronAPI.showOpenDialog({
          properties: ['openFile'],
          filters: [{ name: 'Potree点云js文件', extensions: ['js'] }]
        });
        if (!result.canceled && result.filePaths.length > 0) {
          const filePath = result.filePaths[0];
          // 自动拷贝到public目录
          const publicUrl = await window.electronAPI.copyToPublic(filePath);
          console.log('文件', publicUrl);
          
          currentFileUrl.value = publicUrl; // 修复：使用ref对象
        }
      }
    }



    const handleFileLoaded = (info) => {
      // 可选：处理加载成功
      console.log('文件加载成功:', info)
    }
    const handleError = (msg) => {
      showError(msg)
    }
    const showError = (msg) => {
      error.value = msg
      setTimeout(() => { error.value = '' }, 5000)
    }
    const clearError = () => { error.value = '' }

    return {
      currentFileName,
      currentFileContent,
      currentFileUrl,
      openFileDialog,
      handleFileLoaded,
      handleError,
      error,
      clearError
    }
  }
}
</script>

<style>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#app {
  height: 100vh;
  display: flex;
  flex-direction: column;
  font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}

.app-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 1rem 2rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  z-index: 1000;
}

.app-header h1 {
  font-size: 1.5rem;
  font-weight: 600;
}

.file-controls {
  display: flex;
  gap: 1rem;
}

.btn {
  padding: 0.5rem 1rem;
  border: none;
  border-radius: 6px;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
}

.btn-primary {
  background: #4CAF50;
  color: white;
}

.btn-primary:hover {
  background: #45a049;
  transform: translateY(-1px);
}

.btn-secondary {
  background: #2196F3;
  color: white;
}

.btn-secondary:hover {
  background: #1976D2;
  transform: translateY(-1px);
}

.btn-outline {
  background: transparent;
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.3);
}

.btn-outline:hover {
  background: rgba(255, 255, 255, 0.1);
  transform: translateY(-1px);
}

.app-main {
  flex: 1;
  position: relative;
}

.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 2000;
}

.modal-content {
  background: white;
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  max-width: 600px;
  width: 90%;
  max-height: 80vh;
  overflow: hidden;
}

.modal-header {
  padding: 1rem 1.5rem;
  border-bottom: 1px solid #eee;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header h3 {
  font-size: 1.2rem;
  font-weight: 600;
}

.close-btn {
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: #666;
  padding: 0;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background-color 0.2s;
}

.close-btn:hover {
  background: #f0f0f0;
}

.modal-body {
  padding: 1.5rem;
  max-height: 60vh;
  overflow-y: auto;
}

.empty-state {
  text-align: center;
  color: #666;
  padding: 2rem;
}

.file-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.file-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.75rem;
  border: 1px solid #eee;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.file-item:hover {
  background: #f8f9fa;
  border-color: #007bff;
}

.file-info {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  flex: 1;
}

.file-name {
  font-weight: 600;
  color: #333;
}

.file-path {
  font-size: 0.8rem;
  color: #666;
  font-family: monospace;
}

.file-date {
  font-size: 0.8rem;
  color: #999;
}

.remove-btn {
  background: none;
  border: none;
  color: #dc3545;
  cursor: pointer;
  font-size: 1.2rem;
  padding: 0.25rem;
  border-radius: 50%;
  width: 30px;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background-color 0.2s;
}

.remove-btn:hover {
  background: #f8d7da;
}

.error-toast {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 3000;
  max-width: 400px;
}

.error-content {
  background: #f8d7da;
  color: #721c24;
  padding: 1rem;
  border-radius: 6px;
  border: 1px solid #f5c6cb;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.error-icon {
  font-size: 1.2rem;
}

.error-text {
  flex: 1;
  font-size: 0.9rem;
}

.error-close {
  background: none;
  border: none;
  color: #721c24;
  cursor: pointer;
  font-size: 1.2rem;
  padding: 0;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background-color 0.2s;
}

.error-close:hover {
  background: rgba(220, 53, 69, 0.1);
}
</style>
